<?php
require_once 'Entrepot.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *  $mysq_format_date = date("Y-m-d H:i:s");
 */

/**
 * Description of BDDEntrepot
 *
 * @author Gael
 */
class BDDEntrepot  extends Entrepot {
    
    function __construct($id, $site, $jour=NULL, $type=NULL, $es=NULL) {
        if($jour==NULL && $type==NULL){
            $this->articles = $this->byId($id);
            parent::__construct($id, $site);
        }
        else{
            $this->articles = $this->jour($id, $type, $jour, $es);
            parent::__construct($id, $site);
        }
    }
    
    
       public function byId($id){
        try{
            $db= getPdo();
             try{ // on essai de lancer une transaction au cas ou elle serait déjà lancer dans une autre fonction
                $db->beginTransaction();
                $commit = true;
            }catch(PDOException $e){
                $commit = false;
                //echo'capture de lancement de transaction :'. $e->getMessage();
            }
            
            $rep2 = $db->query("select * from entreestock inner join article on entreestock.idarticle = article.id 
                    inner join typearticle on article.idtypearticle = typearticle.id where identrepot=$id and 
                    idarticle NOT IN( select DISTINCT idarticle from sortiestock)" );
            $tabart = array();
            $i = 0;
            while($data2 =$rep2->fetch()){

               $tabart[$i]=new Article($data2['idarticle'], $data2['codebarre'], 
                       new TypeArticle($data2['idtypearticle'], $data2['nomtype'], $data2['modele']));
               $i++;
            }
            echo $i;
            $rep2->closeCursor();          
            
            return $tabart;
            if($commit){$db->commit();}
        }catch(Exception $e){
            $db->rollBack();
            die('erreur requete'.$e->getMessage());
            return false;
        }
    }
    
    public function jour($id, $type, $jour, $es){
        if($es == "entree"){
            try{
                $db= getPdo();
                $rep2 = $db->query("select * from entreestock inner join article on entreestock.idarticle = article.id 
                        inner join typearticle on article.idtypearticle = typearticle.id 
                        where idtypearticle =$type and dateentree ='$jour'" );
                $tabart = array();
                $i = 0;
                while($data2 =$rep2->fetch()){

                   $tabart[$i]=new Article($data2['idarticle'], $data2['codebarre'], 
                           new TypeArticle($data2['idtypearticle'], $data2['nomtype'], $data2['modele']));
                }
                echo $i;
                $rep2->closeCursor();          

                return $tabart;
            }catch(Exception $e){
                die('erreur requete'.$e->getMessage());
                return false;
            }
        }
        else{
            try{
                $db= getPdo();
                $rep2 = $db->query("select * from sortiestock inner join article on sortiestock.idarticle = article.id 
                        inner join typearticle on article.idtypearticle = typearticle.id 
                        where idtypearticle =$type and datesortie ='$jour'" );
                $tabart = array();
                $i = 0;
                while($data2 =$rep2->fetch()){

                   $tabart[$i]=new Article($data2['idarticle'], $data2['codebarre'], 
                           new TypeArticle($data2['idtypearticle'], $data2['nomtype'], $data2['modele']));
                }
                echo $i;
                $rep2->closeCursor();          

                return $tabart;
            }catch(Exception $e){
                die('erreur requete'.$e->getMessage());
                return false;
            }
        }
    }
    
    public function all(){
        try{
            $db= getPdo();
            $tab = array();
            
            $rep = $db->query("select * from entrepot  " );
            while($data =$rep->fetch()){
                $rep2 = $db->query("select idarticle from entreestock where identrepot=".$data['id']." AND idarticle NOT IN( select DISTINCT idarticle from sortiestock) " );
                $tabart = array();
                while($data2 =$rep2->fetch()){
                   $tabart[]=BDDArticle::byId($data2['idarticle']);
                    
                }
                
                $tab[$data['id']]=new BBDEntrepot($data['id'], $data['site'],$tabart);
               
                
            }
            $rep->closeCursor();
            $rep2->closeCursor();
            return $tab;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
        }
    } 
    static function getArticlesSorties($date,$type){
        
        try{
            $db= getPdo();
            $tab = array();
             try{ // on essai de lancer une transaction au cas ou elle serait déjà lancer dans une autre fonction
                $db->beginTransaction();
                $commit = true;
            }catch(PDOException $e){
                $commit = false;
                //echo'capture de lancement de transaction :'. $e->getMessage();
            }
            
            $rep = $db->query("select idarticle from sortiestock ss
                                                join article a on a.id=ss.idarticle
                                                join typearticle ta on ta.id=a.idtypearticle
where ta.nomtype='$type' AND datesortie='$date' ");
           //return $rep->columnCount();
            while($data = $rep->fetch()){
                echo $data['idarticle'];
                $tab[] = BDDArticle::byId($data['idarticle']);
            }
            if($commit){ $db->commit();}
            return $tab;
     }catch(Exception $e){
         die('probleme requete'.$e->getMessage());
     }
        
    }

    static function getArticlesEntrees($date,$type){
        
        try{
            $db= getPdo();
            $tab = array();
            
            $rep = $db->query("select idarticle from entreestock ss
                                                join article a on a.id=ss.idarticle
                                                join typearticle ta on ta.id=a.idtypearticle
where ta.nomtype='$type' AND ss.dateentree='$date' ");
           //return $rep->columnCount();
            
            while($data = $rep->fetch()){
                $tab[] = BDDArticle::byId($data['idarticle']);
            }
            //p6($tab);
            return $tab;
     }catch(Exception $e){
         die('probleme requete'.$e->getMessage());
     }
        
    }
    
    static function byIdEntrepot($id){
        try{
            $db= getPdo();
            $rep = $db->query("select * from entrepot where id=$id" );
            while($data =$rep->fetch()){
                $ta= new BDDEntrepot($id, $data['site']);
            }
            
            $rep->closeCursor();
            return $ta;
        }catch(Exception $e){
            die('erreur requete'.$e->getMessage());
            return false;
        }
    }

}

?>
